package com.use.utils.demo;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test01 {

    public static void main(String[] args) {
        String uri = "http://example.aliyundoc.com/ping?foo=bar"; // original uri
        String key = "<input private key>"; // private key of authorization
        long exp = System.currentTimeMillis() / 1000 + 1 * 3600; // expiration time: 1 hour after current time

        // 调用鉴权方式A
        String authuri = aAuth(uri, key, exp);
        System.out.println("URL : " + uri);
        System.out.println("AUTH: " + authuri);
    }

    public static String md5sum(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(src.getBytes("utf-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException | java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 鉴权方式A
    public static String aAuth(String uri, String key, long exp) {
        Pattern p = Pattern.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$");
        Matcher m = p.matcher(uri);
        if (!m.find()) {
            return null;
        }
        String scheme = m.group(1);
        String host = m.group(2);
        String path = m.group(3);
        String args = m.group(4);

        if (scheme == null) scheme = "http://";
        if (path == null) path = "/";
        if (args == null) args = "";

        String rand = "0"; // "0" by default, other value is ok
        String uid = "0";  // "0" by default, other value is ok

        String sstring = path + "-" + exp + "-" + rand + "-" + uid + "-" + key;
        String hashvalue = md5sum(sstring);
        String auth_key = exp + "-" + rand + "-" + uid + "-" + hashvalue;

        if (args.contains("?")) {
            return scheme + host + path + args + "&auth_key=" + auth_key;
        } else {
            return scheme + host + path + args + "?auth_key=" + auth_key;
        }
    }

    // 鉴权方式B
    public static String bAuth(String uri, String key, long exp) {
        Pattern p = Pattern.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$");
        Matcher m = p.matcher(uri);
        if (!m.find()) {
            return null;
        }
        String scheme = m.group(1);
        String host = m.group(2);
        String path = m.group(3);
        String args = m.group(4);

        if (scheme == null) scheme = "http://";
        if (path == null) path = "/";
        if (args == null) args = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String nexp = sdf.format(new Date(exp * 1000));

        String sstring = key + nexp + path;
        String hashvalue = md5sum(sstring);

        return scheme + host + "/" + nexp + "/" + hashvalue + path + args;
    }

    // 鉴权方式C
    public static String cAuth(String uri, String key, long exp) {
        Pattern p = Pattern.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$");
        Matcher m = p.matcher(uri);
        if (!m.find()) {
            return null;
        }
        String scheme = m.group(1);
        String host = m.group(2);
        String path = m.group(3);
        String args = m.group(4);

        if (scheme == null) scheme = "http://";
        if (path == null) path = "/";
        if (args == null) args = "";

        String hexexp = Long.toHexString(exp);
        String sstring = key + path + hexexp;
        String hashvalue = md5sum(sstring);

        return scheme + host + "/" + hashvalue + "/" + hexexp + path + args;
    }
}
